<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<script>
// 简单版
const pending = 'pending'
const fulfilled = 'fulfilled'
const rejected = 'rejected'

class MyPromise {
    constructor (fn) {
        fn(this.resolved, this.rejected)
    }
    status = pending
    queue = []

    resolved = (value) => {
        this.status = fulfilled
        console.log('aaa:', value)
        console.log('this.status:', this.status)
        let fn = this.queue.shift()
        fn(value)
    }
    rejected = () => {
        this.status = rejected
    }
    then = (fn) => {
        if (this.status === pending) {
            this.queue.push(fn)
        }
    }
} 

console.log('start')
let promise1 = new MyPromise ((resolved, rejected) => {
    setTimeout(() => {
        console.log('myPromise')
        resolved('success')
    }, 3000);
})
promise1.then((res) => {
    console.log('then', res)
}, (err) => {
    console.log('err', err)
})

</script>
</body>
</html>